package com.woniuxy.buyer.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniuxy.buyer.entity.Product;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 商品Mapper
 */
@Mapper
public interface ProductMapper extends BaseMapper<Product> {

    /**
     * 查询在线商品（包含分类和商家信息）
     */
    @Select("SELECT p.*, pc.name as category_name, u.nickname as seller_name " +
            "FROM products p " +
            "LEFT JOIN product_categories pc ON p.category_id = pc.id " +
            "LEFT JOIN users u ON p.seller_id = u.id " +
            "WHERE p.status = 'ONLINE' AND p.stock > 0 " +
            "ORDER BY p.create_time DESC")
    List<Product> selectOnlineProducts();

    /**
     * 根据分类查询商品
     */
    @Select("SELECT p.*, pc.name as category_name, u.nickname as seller_name " +
            "FROM products p " +
            "LEFT JOIN product_categories pc ON p.category_id = pc.id " +
            "LEFT JOIN users u ON p.seller_id = u.id " +
            "WHERE p.status = 'ONLINE' AND p.stock > 0 AND p.category_id = #{categoryId} " +
            "ORDER BY p.create_time DESC")
    List<Product> selectProductsByCategory(Long categoryId);
}
